import {orderListApi,
        orderDetailApi,
        delOrderApi,
        upLoadApi,
        evaluateTwo,
        cancleOrderApi,
        finishOrderApi,
        payListApi,
        recommendsApi,
        barterApi,
        refundApi,
        returnApi,
        bartersListApi,
        returnListApi,
        EvaluateApi,
        h5WeiXin,
        YEverification,
        alipayPayment,
        addCartOrBuy,
        returnDetailApi,
        refundDetailApi,
        refundListApi,
        againApi,
        barterDeliveryApi,
        returnDeliveryApi,
        orderFinishApi,
        cartApi,
        barterSendApi,
        } from './api';
import { Toast } from 'antd-mobile';
import {Route, routerRedux} from 'dva/router';
import {meberDetailApi} from "../my/api";


export default {
    namespace: 'orderList',
    state: {
        orderList:[],
        orderDetail:[],
        orderGoodsEntityList:[],
        paymentEntities:[],
        listShopBarterEntity:[],
        hasMore:true,
        imgUrl:[],
        filter:{
            searchType:"",
            keyword:"",
            pageNo:1,
            brandId:"",
            areaId:"",
            specFilter:"",
            sortField:"",
            sortOrder:"",
            maximumPrice:"",
            minimumPrice:"",
            identification:"1",
            storeId:""
        },
        returnDetail: {},
        weixinPay: {},
        refundDetail: {},

        listDataLz: {},
        listLz: [],

        active: 0,
    },
    reducers:{
        save(state, { data }) {
            return { ...state, ...data };
        },
    },
    effects: {
        *OrderListEFF({ obj }, { put, call ,select}) {
            Toast.loading('加载中...', 3);
            const orderlist = yield select((state)=>state.orderList);
            const result = yield call(orderListApi, obj);
            if ( result.code == 200 ) {
                if( result.ret !='' ){
                    yield put({ type: 'save', data: {orderList: result.ret.list || [],pageSize:result.ret.pageSize,filter:{...orderlist.filter}} });
                    Toast.hide();
                }else {
                    yield put({ type: 'save', data: { hasMore: false }});
                }
                return { r:true };
            } else {

            }
        },
        *evaluateEFF({ obj }, { put, call }) {
            const result = yield call(evaluateTwo, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result;
        },
        // Lz add 。。。。。
        *ReturnListEFF({ obj }, { put, call ,select}) {
            Toast.loading('加载中...', 3);
            console.log('ReturnListEFF');
            const listLz = yield select(state => state.listLz);
            const result = yield call(returnListApi, obj);
            console.log(result, "ReturnListEFF.................");
            if ( result.code == 200 ) {
                if( result.ret.totalRecords !='' ){
                    yield put({ type: 'save', data: {listLz: result.ret.list || [], hasMore:result.ret.list.length==20, listDataLz: result.ret} });
                    Toast.hide();
                }else {
                    yield put({ type: 'save', data: { hasMore: false }});
                }
                return result;
            } else {

            }
        },
        *RefundListEFF({ obj }, { put, call ,select}) {
            Toast.loading('加载中...', 3);
            const listLz = yield select((state)=>state.listLz);
            const result = yield call(refundListApi, obj);
            console.log(result, "RefundListEFF");
            if ( result.code == 200 ) {
                if( result.ret.totalRecords != '' ){
                    yield put({ type: 'save', data: {listLz: result.ret.listShopBarterEntity || [], hasMore:result.ret.listShopBarterEntity.length==20,listDataLz: result.ret} });
                    Toast.hide();
                }else {
                    yield put({ type: 'save', data: { hasMore: false }});
                }
                return result;
            } else {

            }
        },

        *upLoadEFF({ obj }, { put,select, call }) {
            Toast.loading('加载中...', 3);
            const orderlist = yield select((state)=>state.orderList);
            const result = yield call(upLoadApi,obj);
            orderlist.imgUrl.push(result.ret.url)
            return result;
        },
        *addCartOrBuyEFF({ obj }, { put, call }) {
            const result = yield call(addCartOrBuy,obj);
            return result;
        },
        *barterEFF({ obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(barterApi,obj);
            return result;
        },
        *refundEFF({ obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(refundApi,obj);
            return result;
        },
        *returnEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(returnApi,obj);
            return result;
        },

        // 退换货发货
        *returnSendEFF({obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            // 0  换货退货，  1退货换货
            console.log(obj, '退换货发货   状态');
            const result = yield call(obj.type == 0 ? barterDeliveryApi : returnDeliveryApi, obj);
            return result;
        },

        // *returnListEFF({obj }, { put, call }) {
        //     Toast.loading('加载中..',3);
        //     const result = yield  call(returnListApi,obj);
        //     if(result.code == 200){
        //         yield put({type:'save',data:{returnList:result.ret.list|| []}});
        //         Toast.hide();
        //     }else{
        //         Toast.fail(result.msg,1.5)
        //     }
        // },
        *bartersListEFF({obj }, { put, call }) {
            Toast.loading('加载中..',3);
            const result = yield  call(bartersListApi,obj);
            if(result.code == 200){
                yield put({type:'save',data:{listShopBarterEntity:result.ret.listShopBarterEntity|| []}});
                Toast.hide();
            }else{

            }
        },
        *OrderDetailEFF({obj}, {put,call }){
            Toast.loading('加载中..',3);
            const result = yield  call(orderDetailApi,obj);
            if(result.code == 200){
                yield put({type:'save',data:{orderDetail:result.ret,orderGoodsEntityList:result.ret.orderGoodsEntityList || []}});
                Toast.hide();
            }else{

            }
        },
        *DelOrderEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(delOrderApi,obj);
            return result;
        },

        // 订单确认收货
        *orderFinishEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(orderFinishApi, obj);
            return result;
        },

        *EvaluateEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(EvaluateApi,obj);
            return result;
        },
        *CancleOrderEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(cancleOrderApi,obj);
            return result;
        },
        *FinishOrderEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(finishOrderApi, obj);
            return result;
        },
        *PayListEFF({ }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(payListApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: {paymentEntities:result.ret.paymentEntities||[]} });
                Toast.hide();
            } else {

            }
        },
        *RecommendsEFF({ }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(recommendsApi);
            if (result.code == 200) {
                yield put({ type: 'save', data: {list:result.ret.list||[]} });
                Toast.hide();
            } else {

            }
        },
        *WeiXinPayment ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(h5WeiXin, obj);
            if (result.code == 200) {
                Toast.success(result.msg + "成功", 1.5,()=>{});
                yield put({ type: 'save', data: {weixinPay: result.ret||[]} });
                Toast.hide();
            } else {
                Toast.fail(result.msg + "失败", 1.5,()=>{});
            }
            return result
        },
        *YEverification ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(YEverification, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data: {weixinPay: result.ret||[]} });
                return result;
                Toast.hide();
            } else {
                Toast.fail(result.msg, 1.5,()=>{});
            }
            return result;
        },
        *ZFBpayment ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(alipayPayment, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data: {weixinPay: result.ret||[]} });
                Toast.hide();
                return result;
            } else {
                Toast.fail(result.msg, 1.5,()=>{});
            }
        },
        *returnDetailEFF ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(returnDetailApi, obj);
            console.log(result, "退换货的接收数据。。。");
            if (result.code == 200) {
                yield put({ type: 'save', data: {returnDetail: result.ret || {}} });
                Toast.hide();
                return result;
            } else {

            }
        },

        *refundDetailEFF ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(refundDetailApi, obj);
            console.log(result, "换货的接收数据。。。");
            if (result.code == 200) {
                yield put({ type: 'save', data: {returnDetail: result.ret || {}} });
                Toast.hide();
                return result;
            } else {

            }
        },
        *againEFF ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(againApi, obj);
            Toast.hide();
            return result;
        },
        *cartEFF ({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(cartApi, obj);
            Toast.hide();
            return result;
        },

        *barterSendEFF({obj}, { put, call }) {
            // console.log(obj);
            Toast.loading('加载中...', 3);
            const result = yield call(barterSendApi, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data:{ invoiceList: result.ret.list[0]} });
                yield put({ type:'settlementEFF' });
                Toast.hide();
            } else {

            }
            return result;
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname == '/orderList') {
                    dispatch({ type: 'OrderListEFF', obj: {page: 1, status: 0} });
                }
                if (pathname.includes('/goodsDetail') || pathname.includes('/pay')) {
                    dispatch({ type: 'RecommendsEFF' });
                }
            });
        },
    },
};
